using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace aichat.Entities
{
    public class ChatMessage : INotifyPropertyChanged
    {
        [Key]
        public int Id { get; set; }
        
        public int UserId { get; set; }
        public virtual UserModel User { get; set; } = null!;
        
        [Required]
        [MaxLength(4000)]
        public string UserMessage { get; set; } = string.Empty;
        
        [Required]
        [MaxLength(4000)]
        private string _aiResponse = string.Empty;
        public string AiResponse 
        { 
            get => _aiResponse;
            set
            {
                if (_aiResponse != value)
                {
                    _aiResponse = value;
                    OnPropertyChanged(nameof(AiResponse));
                }
            }
        }
        
        public string AiModel { get; set; } = string.Empty;
        
        public DateTime CreatedAt { get; set; } = DateTime.Now;
        
        public int TokensUsed { get; set; } = 0;
        
        public bool IsSuccessful { get; set; } = true;
        
        public string? ErrorMessage { get; set; }
        
        // 计算属性：用于UI显示，区分用户消息和AI响应
        public bool IsUser => !string.IsNullOrEmpty(UserMessage);
        
        // INotifyPropertyChanged 实现
        public event PropertyChangedEventHandler? PropertyChanged;
        
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
} 